package com.jy.adscm.operation.hr.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.hr.bean.HrResumeAttachment;
import com.jy.adscm.operation.hr.mapper.mysql.HrResumeAttachmentMapper;
import com.jy.adscm.operation.hr.service.HrResumeAttachmentService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2020-01-07
 */
@Service
public class HrResumeAttachmentServiceImpl extends ServiceImpl<HrResumeAttachmentMapper, HrResumeAttachment> implements HrResumeAttachmentService {

        @Autowired
        JyUserMapper jyUserMapper;
        @Override
        public List<HrResumeAttachment> selectByRid(Integer rid) {
        Wrapper<HrResumeAttachment> wrapper =new EntityWrapper<>();
        wrapper.eq("rid",rid);
        List< HrResumeAttachment> hrResumeAttachments = super.selectList(wrapper);
        for ( HrResumeAttachment hrResumeAttachment : hrResumeAttachments) {
            if (hrResumeAttachment.getCreator()!=null&&!"".equals(hrResumeAttachment.getCreator())){
                JyUser jyUser = jyUserMapper.selectById(hrResumeAttachment.getCreator());
                hrResumeAttachment.setCreatorName(jyUser.getName());
            }
        }
        return hrResumeAttachments;
    }

        @Override
        public Boolean deleteHrResumeAttachments(Integer id, Token token) throws AdscmSystemException {
        boolean delete= false;
        HrResumeAttachment hrResumeAttachment = super.selectById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(hrResumeAttachment.getCreator(),null)){
            if (hrResumeAttachment.getCreator().equals(token.getUser().getId().toString())){
                File file=new File(hrResumeAttachment.getPath());
                if (file.exists()){
                    file.delete();
                }
                delete  = super.deleteById(id);
            }else {
                throw  new AdscmSystemException("您不能删除！");
            }
        }
        return delete;
    }


    }
